<?php

namespace app\admin\controller;

use app\admin\model\UserModel;
use think\Console;
use think\Controller;

class User extends Controller
{
    public function welcome1()
    {
        return $this->fetch('/X-admin/welcome1');
    }

    public function user_list()
    {
        return $this->fetch('user_list');
    }


    public function list0()
    {
        $page = input('param.page');
        $limit = input('param.limit');
        $UserModel = new UserModel();
        $datalist = $UserModel->list($page, $limit);
        $count = $UserModel->count();
        $data = [
            'code' => 0,
            'msg' => 'ok',
            'count' => $count,
            'data' => $datalist
        ];
        return $data;
    }
//    public function User_list1()
//    {
//        $UserModel = new UserModel();
//        $UserModel = $UserModel ->list();
//        $this->assign('list',$UserModel);
//        return $this->fetch();
//    }
    public function user_add()
    {
        if (request()->isPost()) {
            $UserModel = new UserModel();
            $data = [
                'user_name' => input('param.user_name'),
                'password' => input('param.password'),
                'tel' => input('param.tel'),
                'nick_name' => input('param.nick_name'),
            ];
            $UserModel->add($data);
        }
        return $this->fetch();
    }

    public function user_edit()
    {
        $UserModel = new UserModel();
        if (request()->isPost()) {
            $id = input('param.user_id');
            $data = [
                'user_name' => input('param.user_name'),
                'password' => input('param.password'),
                'tel' => input('param.tel'),
                'nick_name' => input('param.nick_name'),
            ];
            $UserModel->update($id, $data);
        }
        return $this->fetch();
    }

    public function user_delete()
    {
        if (request()->isGet()) {
            $id = input('param.id');
            $UserModel = new UserModel();
            $UserModel->delete($id);
        }
    }

    public function user_search()
    {
        $username = input('param.username');
        $UserModel = new UserModel();
        $datalist = $UserModel->search($username);
        $data = [
            'code' => 0,
            'msg' => 'ok',
            'count' => 100,
            'data' => $datalist
        ];
        return $data;
    }
}